package demo4.mapper;

import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
import demo4.pojo.User;
import demo4.util.IDBUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@Component
public class UserMapper implements IUserMapper{
    @Autowired
    private IDBUtil dbUtil;
    public int insert(User user)  {
        System.out.println("UserMapper:向数据库插入用户信息");

        try (Connection connection = dbUtil.getConnection();){
            String sql = "INSERT INTO user(username,password,nickname,age)\n"+
                         "VALUES(?,?,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, user.getUsername());
            ps.setString(2,user.getPassword());
            ps.setString(3,user.getNickname());
            ps.setInt(4,user.getAge());
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return 0;
    }
    public User selectUsername(String name)  {
        try (Connection connection = dbUtil.getConnection();){
            String sql = "SELECT id,username,password,nickname,age "+
                         "FROM user "+
                         "WHERE username=?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,name);
            ResultSet rs = ps.executeQuery();
            if(rs.next()){
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setNickname(rs.getString("nickname"));
                user.setAge(rs.getInt("age"));
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<User> selectAll() {
        List<User> list = new ArrayList<>();
        try (Connection conn = dbUtil.getConnection();){
            Statement sta = conn.createStatement();
            String sql = "SELECT id,username,password,age " +
                         "FROM user";
            ResultSet rs = sta.executeQuery(sql);

            while (rs.next()){
                User user = new User(rs.getInt("id"), rs.getString("username"), rs.getString("password"), rs.getString("nickname"), rs.getInt("age"));
                list.add(user);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int change(String name,String pwd) {
        try (Connection connection = dbUtil.getConnection();){
            String sql = "UPDATE user " +
                         "SET password = ? WHERE username = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(2,name);

            ResultSet rs = ps.executeQuery();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
